﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Xml;
using System.IO;

namespace MobileFX
{
    public partial class frmMain : Form
    {
        [DllImport("aygshell.dll")]
        private static extern int SndPlaySync(string path, uint flags);

        public frmMain()
        {
            InitializeComponent();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            PictureBox senderPictureBox = (PictureBox)sender;
            SndPlaySync(senderPictureBox.Tag.ToString(), 0);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(@"\Program Files\MobileFX\default.xml");

            DataTable buttons = dataSet.Tables["Buttons"];

            string imagesFolder = @"\Program Files\MobileFX\images\default\";
            string effectsFolder = @"\Program Files\MobileFX\effects\default\";

            int left = 9;
            int top = 9;

            for (int i = 0; i < buttons.Rows.Count; i++)
            {
                PictureBox pictureBox = new PictureBox();
                pictureBox.Left = left + (i % 4 * 58);
                pictureBox.Top = top + (i/4 * 58);
                pictureBox.Height = 48;
                pictureBox.Width = 48;
                string imageFile = Path.Combine(imagesFolder, buttons.Rows[i]["IconFile"].ToString());
                pictureBox.Image = new Bitmap(imageFile);
                string soundFile = Path.Combine(effectsFolder, buttons.Rows[i]["SoundFile"].ToString());
                pictureBox.Tag = effectsFolder + buttons.Rows[i]["SoundFile"].ToString();
                pictureBox.Click +=new EventHandler(pictureBox_Click);

                this.Controls.Add(pictureBox);
                pictureBox.BringToFront();
            }
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            frmAbout aboutForm = new frmAbout();
            aboutForm.ShowDialog();
        }
    }
}